package web.mb;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import metier.entities.CalculEstimation;
import metier.entities.Pays;
import metier.entities.Transport;
import metier.sess.ICalculEstimationLocal;
import metier.sess.IPaysLocal;
import metier.sess.ITransportLocal;




@ManagedBean
@RequestScoped
public class CalculEstimationMB {
	
	@EJB
	private ICalculEstimationLocal metier;
	
	@EJB
	private IPaysLocal metierp;
	
	@EJB
	private ITransportLocal metiert;
	
	private Integer Id;
	private CalculEstimation calculeEstim=new CalculEstimation();
	private Integer IdCt;
	private List<String> ListTransport=new ArrayList<String>();
	
	
	public Integer getId() {
		return Id;
	}
	
	public void setId(Integer id) {
		Id = id;
	}
	
	public CalculEstimation getCalculeEstim() {
		return calculeEstim;
	}

	public void setCalculeEstim(CalculEstimation calculeEstim) {
		this.calculeEstim = calculeEstim;
	}
	

	
	public List<String> getListTransport() {
		return ListTransport;
	}

	public void setListTransport(List<String> listTransport) {
		ListTransport = listTransport;
	}

	public Integer getIdCt() {
		return IdCt;
	}

	public void setIdCt(Integer idCt) {
		IdCt = idCt;
	}
	

	
	public String Ajouter()
	{
		
		List<Transport> listTra=new ArrayList<Transport>();
		//liste of TransportCat
		for (int i = 0; i < ListTransport.size(); i++) {
			
			listTra.add(metiert.getTransport(Integer.parseInt(ListTransport.get(i))));
		}
		calculeEstim.setListTransport(listTra);
		calculeEstim.setPays(metierp.getPays(IdCt));
		metier.addCalcule(calculeEstim);
		return "ManageCalcule.xhtml";
	}
	
	public String edit(Integer Id)
	{
		this.Id=Id;
		CalculEstimation c=metier.getCalcule(Id);
		
		
		List<Transport> listTra=c.getListTransport();
		for (int i = 0; i < listTra.size(); i++) {
			this.ListTransport.add(listTra.get(i).getIdTransport()+"");
		}
		
		this.IdCt=c.getPays().getIdPays();
	    calculeEstim.setDistance(c.getDistance());
	    calculeEstim.setPoids(c.getPoids());
	    calculeEstim.setPrix(c.getPrix());
	    calculeEstim.setStatut(c.getStatut());
	   
		return "EditCalcule.xhtml";

	}
	
	
	public String Modifier()
	{
		CalculEstimation c=metier.getCalcule(Id);
		
		List<Transport> listTra=new ArrayList<Transport>();
		//liste of TransportCat
		for (int i = 0; i < ListTransport.size(); i++) {
			
			listTra.add(metiert.getTransport(Integer.parseInt(ListTransport.get(i))));
		}
		
		c.setListTransport(listTra);
		
		c.setPays(metierp.getPays(IdCt));
		
		c.setDistance(calculeEstim.getDistance());
	    c.setPoids(calculeEstim.getPoids());
	    c.setPrix(calculeEstim.getPrix());
	    c.setStatut(calculeEstim.getStatut());
		
		metier.updateCalcule(c);
		return "ManageCalcule.xhtml";
	}

	public String Supprimer(Integer Id)
	{
		
		metier.deleteCalcule(Id);
	
		return "succes";
		
	}
	
	
	public List<CalculEstimation> getListCalcule()
	{
		return metier.getAllCalcule();
	}

	
	public List<Pays> getListPays()
	{
		return metierp.getAllPaysActive();
	}
	
	
	
	public List<Transport> getListTransportt()
	{
		return metiert.getAllTransport();
	}
	

}
